import axios from 'axios'
import { Message } from 'element-ui';

const baseURL = '/gles-server/base'

const service = axios.create({
  baseURL,
  timeout: 30 * 60 * 1000
})

service.interceptors.request.use(
  config => {
    // const token = getToken()
    // if (!token) {
    //   return logout()
    // }
    // ...
    return config
  },
  error => {
    Promise.reject(error)
  }
)

service.interceptors.response.use(
  async response => {
    if (response.status === 200) {
      if(response.data.code === 0) {
        return Promise.resolve(response.data.data)
      }else {
        Message.error({ message: response.data.desc || response.data })
        // return Promise.reject(response.data)
      }
    } else {
      Message.error({ message: response.data.desc || response.data })
      // return Promise.reject(response.data)
    }
  },
  error => {
    Message.error({ message:'网络请求异常'})
    return Promise.reject(error)
  }
)


export const Get = (url, params) => {
  return new Promise((resolve, reject) => {
    service
      .get(url, { params })
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}


export function Post(url, params) {
  return new Promise((resolve, reject) => {
    service
      .post(url, params)
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        reject(err)
      })
  })
}

export default service
